//
//  PaperListCell.swift
//  Exam-iOS
//
//  Created by lishengfeng on 2018/3/1.
//  Copyright © 2018年 李胜锋. All rights reserved.
//

import Foundation
import Then
import SnapKit

class PaperListCell: UITableViewCell {
    var titleLabel: UILabel!
    var subTitleLabel: UILabel!
    
    var startBtn: UIButton!
    var sampleBtn: UIButton!
    
    
    var startClick: ((PaperListCell)->Void)?
    var sampleClick: ((PaperListCell)->Void)?
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        initUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func set(title: String?,
             price: Int?,
             itemNum: Int?,
             status: PaperStatus?,
             startClick:((PaperListCell)->Void)?,
             sampleClick:((PaperListCell)->Void)?) {
        
        self.startClick = startClick
        self.sampleClick = sampleClick
        
        titleLabel.text = title
        subTitleLabel.text = "本卷共\(itemNum ?? 0)题"
        
        //已经购买、免费
        if price == 0  ||
            status == .bought ||
            status == .free {
            
            startBtn.setAttributedTitle(nil, for: .normal)
            startBtn.setTitle("开始", for: .normal)
            
            sampleBtn.isHidden = true
            
            startBtn.snp.remakeConstraints({ (make) in
                make.size.equalTo(CGSize.init(width: 87, height: 35))
                make.centerY.equalToSuperview()
                make.trailing.equalTo(-10)
            })
        }
        else {
            sampleBtn.isHidden = false
            
            startBtn.snp.remakeConstraints({ (make) in
                make.size.equalTo(CGSize.init(width: 87, height: 35))
                make.top.equalTo(10)
                make.trailing.equalTo(-10)
            })
            
            sampleBtn.snp.remakeConstraints({ (make) in
                make.size.equalTo(CGSize.init(width: 87, height: 35))
                make.bottom.equalTo(-10)
                make.trailing.equalTo(-10)
            })
            
            let str = "开始 \(price ?? 0)"
            
            
            let attri = NSMutableAttributedString.init(string: str)
            let attch = NSTextAttachment.init()
            //表情图片
            attch.image = UIImage.init(named: "Coin_small")
            //设置图片大小
            attch.bounds = CGRect.init(x: 2, y: -2, width: 14, height: 14)
            //创建带有图片的富文本
            let attchStr = NSAttributedString.init(attachment: attch)
            //在文字后面添加图片
            attri.append(attchStr)
            
            startBtn.setAttributedTitle(attri, for: .normal)
            
        }
    }
    
    func initUI() {
        contentView.backgroundColor = UIColor.clear
        backgroundColor = UIColor.clear
        
        let containerView = UIView().then { (view) in
            view.layer.borderWidth = 0.5
            view.backgroundColor = UIColor.white
            view.layer.borderColor = UIColor.lsf_color16(0xd3d3d3).cgColor
            view.clipsToBounds = true
            view.layer.cornerRadius = 4
            
            contentView.addSubview(view)
            view.snp.makeConstraints({ (make) in
                make.height.equalTo(100)
                make.edges.equalTo(UIEdgeInsetsMake(5, 10, 5, 10))
            })
        }
        
        titleLabel = UILabel().then({ (label) in
            label.textColor = UIColor.black
            label.numberOfLines = 0
            label.font = UIFont.systemFont(ofSize: 14)
            
            containerView.addSubview(label)
            label.snp.makeConstraints({ (make) in
                make.top.equalTo(18)
                make.leading.equalTo(11)
                make.trailing.lessThanOrEqualToSuperview().offset(-131)
            })
        })
        
        subTitleLabel = UILabel().then({ (label) in
            label.textColor = UIColor.lsf_color16(0x7e7e7e)
            label.font = UIFont.systemFont(ofSize: 12)
            
            containerView.addSubview(label)
            label.snp.makeConstraints({ (make) in
                make.top.equalTo(titleLabel.snp.bottom).offset(9)
                make.leading.equalTo(11)
            })
        })
        
        startBtn = UIButton.init(type: .custom).then({ (btn) in
            btn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
            btn.setTitleColor(UIColor.black, for: .normal)
            btn.setTitle("开始", for: .normal)
            btn.layer.cornerRadius = 4
            btn.clipsToBounds = true
            btn.layer.borderColor = UIColor.lsf_color16(0xd3d3d3).cgColor
            btn.layer.borderWidth = 0.5
            
            containerView.addSubview(btn)
            
            btn.rx.tap.bind {
                self.startClick?(self)
            }.dispose()
        })
        
        
        sampleBtn = UIButton.init(type: .custom).then({ (btn) in
            btn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
            btn.setTitleColor(UIColor.black, for: .normal)
            btn.setTitle("试用", for: .normal)
            btn.layer.cornerRadius = 4
            btn.clipsToBounds = true
            btn.layer.borderColor = UIColor.lsf_color16(0xd3d3d3).cgColor
            btn.layer.borderWidth = 0.5
            
            containerView.addSubview(btn)
        })
        
        
        startBtn.addTarget(self, action: #selector(PaperListCell.startBtnClick), for: .touchUpInside)
        
        
        sampleBtn.addTarget(self, action: #selector(PaperListCell.sampleBtnClick), for: .touchUpInside)
    }
    
    @objc func startBtnClick() {
        self.startClick?(self)
    }
    
    @objc func sampleBtnClick() {
        self.sampleClick?(self)
    }
}
